<?php
namespace Admin\Controller;
use Common\Model\UserGroupModel;
use Common\Model\UserModel;
use Common\Model\UserInfoModel;
use Think\Page;
use Org\Util\SFile;
/**
 * 网站首页信息
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.2
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2017-07-05 16:41:11
 */
class UserController extends BashController{
    /**
     * 获取权限组列表页面
     * 
     * @return void
     * @version v1.0.0
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @adtime 2017-07-06 16:25:14
     */
    public function group_listAction(){
        $group = new UserGroupModel();
        $where = array();
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $where = array(
            'name' => array('like',$where),
        );
        $where['type'] = 2;
        $count = $group->where($where)->getCount();
        $page = new Page($count, 10);
        $list = $group->where($where)->getList($page->firstRow, $page->listRows);
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 添加权限组信息
     * 
     * @return void
     * @version v1.0.0
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @adtime 2017-07-06 17:50:51
     */
    public function group_addAction(){
        $group = new UserGroupModel();
        if(!I('post.')){
            return $this->assign('fix_group',$group->getList(0,9999))->display();
        }
        $group->create_group(I('post.'));
//        if($group->create_group(I('post.'))){
        return $this->success('添加成功',U('group_list'));
//        }
//        return $this->error('添加失败');
    }
    
    /**
     * 修改权限组信息
     * 
     * @return void
     * @version v1.0.0
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @adtime 2017-07-06 18:25:47
     */
    public function group_saveAction(){
        $group = new UserGroupModel();
        $info = $group->where(array('id'=>I('get.id'),'status'=>array('neq',98)))->find();
        if(empty($info)){
            return $this->error('查无此数据');
        }
        if(!I('post.')){
            $this->assign('name',$info['name']);
            return $this->display('group_add');
        }
        $group->edit_info(I('get.id'),I('post.'));
//        if($group->edit_info(I('get.id'),I('post.'))){
        (new \Common\Model\LogModel())->update_admin_date('user_group',I('post.'),$info);
        return $this->success('修改成功',U('group_list'));
//        }
//        return $this->error('添加失败');
    }
    
    /**
     * 删除权限组信息
     * 
     * @return void
     * @version v1.0.0
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @adtime 2017-07-06 19:37:53
     */
    public function group_delAction(){
        $group = new UserGroupModel();
        $info = $group->where(array('id'=>I('get.id'),'status'=>array('neq',98)))->find();
        if(empty($info)){
            return $this->error('查无此数据');
        }
        $group->setDelete($info['id']);
//        if($group->setDelete($info['id'])){
        (new \Common\Model\LogModel())->delete_admin_log($info['id'],'user_group');
        return $this->success('删除成功');
//        }
//        return $this->error('删除失败');
    }
    
    /**
     * 为权限组进行授权
     * 
     * @return void
     * @version v1.0.0
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @adtime 2017-07-06 21:11:06
     */
    public function group_jurisdictionAction(){
        $group = new UserGroupModel();
        $info = $group->where(array('id'=>I('get.id'),'status'=>array('neq',98)))->find();
        if(!I('post.')){
            $this->assign('g_info', $info);
            $this->assign('group_list',C('MENU_CONFIG.MENU_CONFIG_Jurisdiction'));
            return $this->display();
        }
        (new SFile())->rm_dirs(RUNTIME_PATH);// 清理缓存
        $data = array('content'=> implode(',', I('post.content')));
        $group->where(array('id'=>I('get.id')))->save($data);
//        if($group->where(array('id'=>I('get.id')))->save($data)){
        (new \Common\Model\LogModel())->update_admin_date('group',$data,$info);
        return $this->success('更新成功',U('group_list'));
//        }
//        return $this->error('更新失败');
    }
    
    /**
     * 获取用户列表信息
     * @return type
     */
    public function user_listAction(){
        $user = new UserModel();
        $where = array();
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $where = array(
            'username|email|phone' => array('like',$where),
        );
        $count = $user->where($where)->getCount();
        $page = new Page($count, 10);
        $group = new UserGroupModel();
        $list = $user->where($where)->getList($page->firstRow, $page->listRows);
        foreach($list as $k => $v){
            $list[$k] = $user->get_info($v['id']);
            $list[$k]['admin_group_info'] = $group->get_info($list[$k]['admin_group']);
            $list[$k]['home_group_info'] = $group->get_info($list[$k]['home_group']);
            $list[$k]['api_group_info'] = $group->get_info($list[$k]['api_group']);
        }
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 添加用户操作
     * @return type
     */
    public function user_addAction(){
        if(I('post.')){
            $user = new UserModel();
            $post = I('post.');
            foreach ($post as $k => $v){
                if(empty($v)){
                    unset($post[$k]);
                }
            }
            $post['rand_code'] = $user->create_rand_code();
            $post['password'] = pass($post['password'],$post['rand_code']);
            $uid = $user->create_info($post);
//            if($uid){
            (new \Common\Model\LogModel())->create_oth_log($uid, 'user');
            return $this->success('添加成功',U('user_list'));
//            }
//            return $this->error('系统错误');
        }
        $group_list = (new UserGroupModel())->getListGroup();
        $this->assign('group_list',$group_list);
        return $this->display();
    }
    
    /**
     * 删除用户操作【如果用户的admin分组为1则禁止删除】
     * @return type
     */
    public function user_delAction(){
        $user = new UserModel();
        $info = $user->get_info(I('get.id'));
        if($info['admin_group'] == 1){
            return $this->error('后台超级权限组用户禁止删除');
        }
        $user->setDelete(I('get.id'));
//        if($user->setDelete(I('get.id'))){
        (new \Common\Model\LogModel())->delete_admin_log(I('get.id'),'user');
        return $this->success('删除成功',U('user_list'));
//        }
//        return $this->error('系统错误');
    }
    
    /**
     * 修改用户详情
     */
    public function user_saveAction(){
        $user = new UserModel();
        $info = $user->get_info(I('get.id'));
        if(I('post.')){
            $post = I('post.');
            foreach($post as $k => $v){
                if(empty($v)){
                    unset($post[$k]);
                }
            }
            if(!empty($post['password'])){
                $post['rand_code'] = $user->create_rand_code();
                $post['password'] = pass($post['password'],$post['rand_code']);
            } else {
                unset($post['password']);
            }
            if(empty($post['admin_group_leader'])){
                $post['admin_group_leader'] = 0;
            }
            $user->where(array('id'=>I('get.id')))->save($post);
            (new UserInfoModel())->where(array('id'=>I('get.id')))->save($post);
            // 清空缓存
            (new SFile())->rm_dirs(RUNTIME_PATH);
            return $this->success('用户资料更新成功',U('user_list'));
        }
        $this->assign('u_info',$info);
        $group_list = (new UserGroupModel())->getListGroup();
        $this->assign('group_list',$group_list);
        return $this->display();
    }
    
    /**
     * 获取所属业务员列表【ajax获取】
     */
    public function get_user_admin_listAction(){
        $key = I('get.key',NULL,'trim');
        $model = new UserInfoModel();
        $return_info = array();
        $lists = $model
                ->join('INNER JOIN `'.C('DB_PREFIX').'user` ON `'.C('DB_PREFIX').'user_info`.`id` = `'.C('DB_PREFIX').'user`.`id`')
                ->where(array('nickname|username'=>array('like','%'.$key.'%'),'admin_group'=>2))
                ->select();
        foreach($lists as $v){
            $return_info[] = array(
                'id' => $v['id'],
                'text' => $v['username'].' - '.$v['nickname'],
            );
        }
        echo json_encode($return_info);exit;
    }
}